package com.bidevalution.core.util;

import com.bidevalution.core.enums.ApiEnum;
import com.bidevalution.model.BlData;
import com.qtp.core.util.*;
import org.apache.cxf.endpoint.Client;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * webService相关工具类
 * @author huyiqi
 * @date 2019/11/11
 */
public class WebServiceUtils {

    private static final ClassLoader cl = Thread.currentThread().getContextClassLoader();

    private static final byte[] iv = { 0x12, 0x34, 0x56, 0x78, (byte)0x90, (byte)0xAB, (byte)0xCD, (byte)0xEF };


    //投标文件导入反馈
    public static void main(String[] args) {
        String webUrl = "http://115.238.53.108:8081/DataService.asmx?wsdl";
        String method = "BackPutInResult";
        try {
            String code = DesUtil.encrypt("01141120191211021", "20181108", iv);
            String guid = DesUtil.encrypt("224f445d-68a6-44ab-bc4c-21c61166f8e0", "20181108", iv);
            String succ = DesUtil.encrypt("1", "20181108", iv);
            String status = DesUtil.encrypt("200", "20181108", iv);
            String remark = DesUtil.encrypt("成功", "20181108", iv);
            String result = GsonUtil.toJsonWithGson(WebServiceUtils.callWebSVObj(webUrl, method,
                    "8B4FAE4A-9A92-4EDA-8D7B-0A5B12E8BF1D", code, guid, succ, status, remark));
            String xml = DesUtil.decrypt(result, "20181108", iv);
            System.out.println(xml);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //专家验证
//    public static void main(String[] args) {
//        String name = "郭立人";
//        String id = "330103196602100710";
//        String webUrl = "http://115.233.209.150:88/jsjgauditing2/ExportzjInfoService.asmx?wsdl";
//        String method = "GetExportInfoByCardIDName";
//        try {
//            String result = GsonUtil.toJsonWithGson(WebServiceUtils.callWebSVObj(webUrl, method, id, "", name));
//            Map<String, Object> root = GsonUtil.jsonToMapWithGson(result);
//            System.out.println(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }

//        重置
//    public static void main(String[] args) {
//        String webUrl = "http://115.238.53.108:8081/DataService.asmx?wsdl";
//        String method = "UpdateTenderBiddingState";
//        try {
//            String code = DesUtil.encrypt("01141120191123023", "20181108", iv);
//            String result = GsonUtil.toJsonWithGson(WebServiceUtils.callWebSVObj(webUrl, method,
//                    "8B4FAE4A-9A92-4EDA-8D7B-0A5B12E8BF1D", code));
//            String xml = DesUtil.decrypt(result, "20181108", iv);
//            System.out.println(xml);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//    public static void main(String[] args) throws Exception {
////////        重推
//        String webUrl = "http://115.238.53.109:10009/Service1.asmx?wsdl";
//        String methodName = "GetProjectBLUpdate";
//        try {
//            BlData blData = new BlData("01141120191116021");
//            blData.addRow("123", "测试单位", 100000D, "2019-11-22 16:33:33", "合格", "备注", new Date());
//            String sectionCode = Base64Util.encryptBase64("01141120191116021");
//            String data = Base64Util.encryptBase64(blData.toString());
//
////            String r = DesUtil.encrypt("1", "20181108", iv);
////            String errorCode = DesUtil.encrypt("200", "20181108", iv);
////            String remark = DesUtil.encrypt("测试", "20181108", iv);
//            String result = WebServiceUtils.callWebSV(webUrl, methodName, "1E2GDBE2-C597-94CC-ABBC-93B18A163D52"
//                    , sectionCode, data);
////            String xml = DesUtil.decrypt(result, "20181108", iv);
//            System.out.println(result);
////            System.out.println(xml);
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//
////        String webUrl = "http://115.233.209.212:906/Getdata.asmx?wsdl";
////        String methodName = "AcceptServiceBankReturn";
////        try {
////            String result = WebServiceUtils.callWebSV(webUrl, methodName, "qzhou","987654123","01141120191122031");
//////            String xml = DesUtil.decrypt(result, "20181108", iv);
////            System.out.println(result);
//////            System.out.println(xml);
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
//    }

//    //投标反馈抓取中标结果信息
//    public static void main(String[] args) {
//        String webUrl = "http://115.238.53.109:10037/DataService.asmx?wsdl";
//        String methodName = "BackPutInResult";
//        try {
//            String sectionCode = DesUtil.encrypt("01141120191125011", "20181108", iv);
//            String GUID = DesUtil.encrypt("8c3ec028-768e-4bbb-bd94-becd8b57f0ae", "20181108", iv);
//            String backResult =  DesUtil.encrypt("1", "20181108", iv);
//            String errorCode =  DesUtil.encrypt("200", "20181108", iv);
//            String response =  DesUtil.encrypt("成功", "20181108", iv);
//
//            String result = WebServiceUtils.callWebSV(webUrl, methodName, "8B4FAE4A-9A92-4EDA-8D7B-0A5B12E8BF1D"
//                    , sectionCode, GUID, backResult, errorCode, response);
//
//            String xml = DesUtil.decrypt(result, "20181108", iv);
//            System.out.println(xml);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    public static String callWebSV(String wsdUrl, String operationName, Object... params) throws Exception {
        Thread.currentThread().setContextClassLoader(cl);

        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        Object[] objects;
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke(operationName, params);
        return objects[0].toString();
    }

    public static Object callWebSVObj(String wsdUrl, String operationName, String... params) throws Exception {
        Thread.currentThread().setContextClassLoader(cl);

        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        Object[] objects;
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke(operationName, params);
        return objects[0];
    }
}
